package com.example.carrierapp.ui.person.notice;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.example.carrierapp.base.MVPBaseActivity;
import com.example.carrierapp.bean.home.NoticeDetailBean;
import com.example.carrierapp.bean.home.NoticeListBean;
import com.example.carrierapp.ui.adapter.NoticeAdapter;
import com.example.carrierapp.ui.view.recycleview.HorizontalDividerDecoration;
import com.example.carrierapp.ui.view.recycleview.WrapContentManager;
import com.example.carrierapp.ui.view.recycleview.XRecyclerView;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.ConnectionUtil;
import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.LogUtil;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NoticeActivity extends MVPBaseActivity<NoticeContract.View, NoticePresenter> implements
        NoticeContract.View, XRecyclerView.LoadingListener, NoticeAdapter.OnBtnClickListener {

    @BindView(R.id.ll_empty)
    LinearLayout mEmptyView;
    @BindView(R.id.recycler_view)
    XRecyclerView mRecyclerView;

    private int mPage = 0;
    private boolean mLoadMore;
    private NoticeAdapter mAdapter;
    private NoticeListBean.ItemsBean mClickInfo;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showProgress();
        mPresenter.attachView(this);
        mPresenter.getNoticeList(mPage);
    }

    @Override
    protected NoticePresenter createPresenter() {
        return new NoticePresenter(this);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_notice;
    }

    @Override
    public String getTitleString() {
        return "个人通知";
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        initRecycleView();
    }

    private void initRecycleView() {
        WrapContentManager mLinearLayoutManager = new WrapContentManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.addItemDecoration(new HorizontalDividerDecoration(this, R.drawable.divider_horizontal_2_f3f6fa, DisplayUtil.dip2px(this, 10)));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLoadingMoreEnabled(true);
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingListener(this);
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();

        mAdapter = new NoticeAdapter();
        mAdapter.setOnBtnClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    public void getNoticeListSuccess(NoticeListBean bean) {
        dismissProgress();
        LogUtil.d("NoticeActivity-getNoticeListSuccess-" + bean);
        if (mRecyclerView != null) {
            mRecyclerView.loadMoreComplete();
            mRecyclerView.refreshComplete();
        }
        List<NoticeListBean.ItemsBean> list = bean.getItems();
        LogUtil.d("NoticeActivity-getNoticeListSuccess-" + list.size());
        mEmptyView.setVisibility(View.GONE);
        if (!ConnectionUtil.isNull(list)) {
            if (mLoadMore) {
                mLoadMore = false;
                mAdapter.addDataNotClear(list);
            } else {
                mAdapter.addData(list);
            }
            if (mRecyclerView != null && list.size() < 10) {
                mRecyclerView.setNoMore(true);
            } else {
                mRecyclerView.setNoMore(false);
            }
        } else {
            mRecyclerView.setNoMore(true);
            if (!mLoadMore) {
                mAdapter.clear();
                mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mEmptyView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void getNoticeListFailde(String msg) {
        dismissProgress();
        LogUtil.d("NoticeActivity-getNoticeListFailde-" + msg);
        ToastUtils.showCenterToast(msg, this);
        if (mRecyclerView != null) {
            mRecyclerView.loadMoreComplete();
            mRecyclerView.refreshComplete();
        }
        mEmptyView.setVisibility(mAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    public void getNoticeDetailSuccess(NoticeDetailBean bean) {
        LogUtil.d("NoticeActivity-getNoticeDetailSuccess-" + bean);
        mClickInfo.subTitle=bean.content;
        mClickInfo.isShowDetail=true;
        mAdapter.updataItemData(mClickInfo);
    }

    @Override
    public void getNoticeDetailFailed(String msg) {
        LogUtil.d("NoticeActivity-getNoticeDetailFailed-" + msg);
    }

    @Override
    public void onRefresh() {
        mPage = 0;
        mPresenter.getNoticeList(mPage);
        mLoadMore = false;
    }

    @Override
    public void onLoadMore() {
        mPage++;
        mPresenter.getNoticeList(mPage);
        mLoadMore = true;
    }

    @Override
    public void onDetailClick(NoticeListBean.ItemsBean info) {
        mClickInfo = info;
        ActivityUtil.startNoticeDetail(this,info.id);
//        mPresenter.getNoticeDetail(info.id);
    }
}
